﻿    var model = avalon.define({
        $id: "chat",
        isConnect: false,
        chatData: [],
        formatChatData:[], //group by date
        css: {
            chatHeight: 1000
        },

        formartChatData: function (msg, chatData) {

            this.chatData.push(msg);

            var time = moment(msg.time).format("YYYY-MM-DD HH:mm:ss");

            //console.log("time",time);

            return chatData;
        },

        comment: function (e) {
            if (e.keyCode == 13 && avalon(this).val().trim().length > 0) {
               
            	if (account != undefined) {
            	
	                var msg = {
	                    userId: account.id,
	                    userName: account.nick,
	                    content: avalon(this).val(),
	                    userface:'1.jpg'
	                };
            	}
                avalon(this).val('');
                socket.emit('event', msg);
            }

        }
    });

    model.css.chatHeight = window.screen.availHeight - 140;

    var socket = io.connect('http://123.57.39.61:9191/domain1');

   
    //连接成功
    socket.on('connect', function () {
            model.isConnect = true;
            model.chatData = [];
    });

    //连接断开
    socket.on('disconnect', function () {
        model.isConnect = false;
    });

    //接收到消息
    socket.on('event', function (data) {
        if (data)
        {
            var msg = {
                userId: data.userId,
                userName: data.userName,
                face: '',
                time: data.created,
                content: data.content
            };

            var div = document.getElementsByClassName("pnl_chatBorad")[0];
            model.chatData = model.formartChatData(msg);
            avalon.scan();
            div.scrollTop = div.scrollHeight;
        }
    });
    //avalon.scan();

